<?php

namespace app\modules\admin\controllers;

use app\models\AdminJurisdiction;
use yii\web\Controller;

/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
    public $admin_user = false;
    public $enableCsrfValidation = false;
    public $layout = false;
    public $URL = 'http://www.tyt.com/front/index/index';
    public $FILE_URL = 'http://yii.jump.com';
    public $demo_url = 'http://www.tyt.com';


    public function beforeAction($action)
    {
        parent::beforeAction($action);
        if(empty($_SESSION)) session_start();
        $login = $_SESSION['admin_login']??false;
        if(empty($login)){
            return  $this->redirect(\Yii::$app->urlManager->createUrl(['admin/login/index']));
        }
        $this->admin_user = json_decode($login,true);
        return true;
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function Index()
    {
        $jur = AdminJurisdiction::find()->where(['level'=>0])->asArray()->all();

        return $this->render('/default/index',[
            'data'=>$jur,

        ]);
    }

    public function End()
    {
        return $this->render('/default/end');
    }

    /**
     * @return array
     * 入口 二维码地址
     */
    public function Inlet_QR_Code()
    {
        return [
            [
                'code_url' => $this->demo_url.'/front/index/index',
                'name' => '首页'
            ],
            [
                'code_url' => $this->demo_url.'/front/activity/index',
                'name' => '活动'
            ],
            [
                'code_url' => $this->demo_url.'/front/my/index',
                'name' => '我的'
            ],
            [
                'code_url' => $this->demo_url.'/front/capital_record/index',
                'name' => '我的金币'
            ],
            [
                'code_url' => $this->demo_url.'/front/order/index',
                'name' => '我的订单'
            ],
            [
                'code_url' => $this->demo_url.'/front/gift/index',
                'name' => '我的礼品'
            ],
            [
                'code_url' => $this->demo_url.'/front/refund/index',
                'name' => '申请退款'
            ],
            [
                'code_url' => $this->demo_url.'/front/retrieve/index',
                'name' => '充值未到账'
            ],
        ];
    }

    public function Up_File($file)
    {
        if(empty($file['file0']['name'])){
            return false;
        }
        $name = "up_file/" . rand(00000000, 99999999) . time() . $file['file0']['name'];
        move_uploaded_file($file['file0']['tmp_name'], $name);
        $url = $this->FILE_URL . '/' . $name;
        return $url;
    }

    public function actionTime()
    {
        return $this->render('time');
    }

    public function actionError()
    {
        $url = $_GET[1]['url'] ?? "/admin/index/index";
        $message = $_GET[1]['message'] ?? "出错啦";
        return $this->render('error',[
            'head'=>$this->Index(),
            'jump_url'=>$url,
            'message'=>$message,
        ]);
    }
}
